布线问题--队列式分支界限法

#include <iostream>
#include <queue>
#include <algorithm>
#include <utility>
#include <stack>
using namespace std;

typedef pair<int, int> posi;
queue<posi> q;
const int n = 100;
int graph[n+2][n+2] = {0};
posi start;
posi end;
int best_path = 0;
void initParament()
{

}

void findPath()
{
        if(start.first == end.first && start.second==end.second){
                return;
        }

        for(int i=0; i<=n; i++){
                graph[0][i] = 1;
                graph[n+1][i] = 1;
                graph[i][0] = 1;
                graph[i][n+1] = 1;
        }

        posi offset[5];
        offset[1].first = 1; offset[1].second = 0;
        offset[2].first = 0; offset[1].second = -1;
        offset[1].first = -1; offset[1].second = 0;
        offset[1].first = 0; offset[1].second = 1;

        posi current = start;
        graph[current.first][current.second] = 2;
        while( true ){
                posi here;
                for(int i=1; i<=4; i++){
                        here.first = current.first+offset[i].first;
                        here.second = current.second+offset[i].second;

                        if(graph[here.first][here.second] == 0){
                                q.push(here);
                                graph[here.first][here.second] = graph[current.first][current.second] + 1;

                                if(here.first == end.first && here.second == end.second){
                                        best_path = graph[here.first][here.second];
                                        break;
                                }
                        }
                }

                if(here.first==end.first && here.second==end.second){
                        break;
                }

                if( q.empty() ){
                        break;
                }

                current = q.front();
                q.pop();
        }

        stack<posi> path;
        path.push(end);
        current = end;
        for(int i=best_path; i>2; i--){
                posi here;
                for(int j=1; j<=4; j++){
                        here.first = end.first+offset[j].first;
                        here.second = end.second+offset[j].second;

                        if(graph[here.first][here.second] == i-1){
                                path.push(here);
                                break;
                        }
                }
                current = here;
        }
        path.push(start);

        while( !path.empty() ){
                current = path.top();
                cout << current.first << "," << current.second << endl;
                path.pop();
        }
}

int main()
{
        initParament();

        findPath();

        return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值